def gridSweep(arr):
    for index, elm1 in enumerate(arr):
        for elm2 in arr[:index]:
            yield elm1, elm2

def makeSumEqual(Q, qubits, total_value, lagrange = 0.5):
    for qubit in qubits:
        Q[(qubit, qubit)] += (1 - 2 * total_value) * lagrange

    for qubit1, qubit2 in gridSweep(qubits):
        Q[(qubit1, qubit2)] += 2 * lagrange
    return

def makeExactOne(Q, variables, lagrange = 0.5):
    makeSumEqual(Q, variables, 1, lagrange)
    return

def makeTwoQubitSimilar(Q, qubit1, qubit2, lagrange = 0.5):
    Q[(qubit1, qubit1)] += 1 * lagrange
    Q[(qubit1, qubit2)] -= 2 * lagrange
    Q[(qubit2, qubit2)] += 1 * lagrange
    return

def makeTwoQubitDifferent(Q, qubit1, qubit2, lagrange = 0.5):
    Q[(qubit1, qubit1)] -= 1 * lagrange
    Q[(qubit1, qubit2)] += 2 * lagrange
    Q[(qubit2, qubit2)] -= 1 * lagrange
    return